﻿/******************************************************************************

    Copyright (c) 2010, Sergey Mudrov
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in the
          documentation and/or other materials provided with the distribution.
        * Neither the name of Sergey Mudrov nor the
          names of its contributors may be used to endorse or promote products
          derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL SERGEY MUDROV BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {
        /// <summary>
        /// Gets the video information.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of videos info.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Get(long app_id, string sig, string v, ApiCallback<Video[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));

            return this.SendRequest("video.get", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the video information.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="videos">The videos IDs in format: userid_videoid.</param>
        /// <param name="width">The width of video. Can be 130, 160 (by default), 320. (unnecessary)</param>
        /// <param name="count">The count of items to get. (unnecessary)</param>
        /// <param name="offset">The offset from first found item. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of videos info.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Get(long app_id, string sig, string v, string videos, int width, int count, int offset, ApiCallback<Video[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("videos", videos));
            list.Add(new Parameter("width", width.ToString()));
            list.Add(new Parameter("count", count.ToString()));
            list.Add(new Parameter("ofest", offset.ToString()));

            return this.SendRequest("video.get", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the video information.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="uid">The user ID who's videos will be get.</param>
        /// <param name="width">The width of video. Can be 130, 160 (by default), 320. (unnecessary)</param>
        /// <param name="count">The count of items to get. (unnecessary)</param>
        /// <param name="offset">The offset from first found item. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of videos info.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Get(long app_id, string sig, string v, long uid, int width, int count, int offset, ApiCallback<Video[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("width", width.ToString()));
            list.Add(new Parameter("count", count.ToString()));
            list.Add(new Parameter("ofest", offset.ToString()));

            return this.SendRequest("video.get", callback, list.ToArray());
        }

        /// <summary>
        /// Edits the video information.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="vid">The video ID.</param>
        /// <param name="oid">The owner ID.</param>
        /// <param name="name">The name of the video.</param>
        /// <param name="desc">The description of the video.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Get(long app_id, string sig, string v, long vid, long oid, string name, string desc, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            list.Add(new Parameter("oid", oid.ToString()));
            list.Add(new Parameter("name", name));
            list.Add(new Parameter("desc", desc));

            return this.SendRequest("video.edit", callback, list.ToArray());
        }

        /// <summary>
        /// Copies the video to user page.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="vid">The video ID.</param>
        /// <param name="oid">The owner ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Add(long app_id, string sig, string v, long vid, long oid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            list.Add(new Parameter("oid", oid.ToString()));

            return this.SendRequest("video.add", callback, list.ToArray());
        }

        /// <summary>
        /// Deletes the video from user page.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="vid">The video ID.</param>
        /// <param name="oid">The owner ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Delete(long app_id, string sig, string v, long vid, long oid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            list.Add(new Parameter("oid", oid.ToString()));

            return this.SendRequest("video.delete", callback, list.ToArray());
        }

        /// <summary>
        /// Searches the videos.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="q">The query to search. (unnecessary)</param>
        /// <param name="sort">The sorting key. 1 by duration, 0 by date. (unnecessary)</param>
        /// <param name="hd">The HD key. If !=0 searching only HD videos. (unnecessary)</param>
        /// <param name="count">The count of items to return. (unnecessary)</param>
        /// <param name="offset">The offset from the first match. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Count of videos and video array.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Search(long app_id, string sig, string v, string q, int? sort, int? hd, int? count, int? offset, ApiCallback<SearchedVideo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            if (q != null) list.Add(new Parameter("q", q.ToString()));
            if (sort != null) list.Add(new Parameter("sort", sort.ToString()));
            if (hd != null) list.Add(new Parameter("hd", hd.ToString()));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));

            return this.SendRequest("video.search", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the videos where user was marked.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="uid">The user ID. Current by default. (unnecessary)</param>
        /// <param name="offset">The offset from first match. (unnecessary)</param>
        /// <param name="count">The count of items to return. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of records.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_GetUserVideos(long app_id, string sig, string v, string uid, int? offset, int? count, ApiCallback<UserVideo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            if (uid != null) list.Add(new Parameter("uid", uid.ToString()));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));

            return this.SendRequest("video.getUserVideos", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the comments of video.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="vid">The video ID.</param>
        /// <param name="owner_id">The user ID. Current by default. (unnecessary)</param>
        /// <param name="offset">The offset from first match. (unnecessary)</param>
        /// <param name="count">The count of items to return. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of comments.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_GetUserVideos(long app_id, string sig, string v, long vid, long? owner_id, int? offset, int? count, ApiCallback<Comment[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            if (owner_id != null) list.Add(new Parameter("owner_id", owner_id.ToString()));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));

            return this.SendRequest("video.getComments", callback, list.ToArray());
        }

        /// <summary>
        /// Creates the comment for video.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="vid">The video ID.</param>
        /// <param name="owner_id">The user ID. (unnecessary)</param>
        /// <param name="message">The message of the comment.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>New comment ID.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_CreateComment(long app_id, string sig, string v, long vid, long? owner_id, string message, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            if (owner_id != null) list.Add(new Parameter("owner_id", owner_id.ToString()));
            list.Add(new Parameter("message", message.ToString()));

            return this.SendRequest("video.createComment", callback, list.ToArray());
        }

        /// <summary>
        /// Edits the video comment.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="owner_id">The user ID. Current user by default. (unnecessary)</param>
        /// <param name="cid">The comment ID.</param>
        /// <param name="message">The message. Length > 1.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_EditComment(long app_id, string sig, string v, long? owner_id, long cid, string message, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("cid", cid.ToString()));
            if (owner_id != null) list.Add(new Parameter("owner_id", owner_id.ToString()));
            list.Add(new Parameter("message", message.ToString()));

            return this.SendRequest("video.editComment", callback, list.ToArray());
        }

        /// <summary>
        /// Deletes the video comment.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="owner_id">The user ID. Current user by default. (unnecessary)</param>
        /// <param name="cid">The comment ID.</param>        
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_DeleteComment(long app_id, string sig, string v, long? owner_id, long cid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("cid", cid.ToString()));
            if (owner_id != null) list.Add(new Parameter("owner_id", owner_id.ToString()));

            return this.SendRequest("video.deleteComment", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the video tags.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="owner_id">The user ID. Current user by default. (unnecessary)</param>
        /// <param name="vid">The video ID.</param>        
        /// <param name="callback">The callback.</param>
        /// <returns>Array of tags.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_GetTags(long app_id, string sig, string v, long? owner_id, long vid, ApiCallback<Tag[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            if (owner_id != null) list.Add(new Parameter("owner_id", owner_id.ToString()));

            return this.SendRequest("video.getTags", callback, list.ToArray());
        }

        /// <summary>
        /// Add the video tag.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="owner_id">The user ID. Current user by default. (unnecessary)</param>
        /// <param name="vid">The video ID.</param>  
        /// <param name="uid">The user ID that must be marked on video.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>New tag ID.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_PutTag(long app_id, string sig, string v, long? owner_id, long vid, long uid, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));
            if (owner_id != null) list.Add(new Parameter("owner_id", owner_id.ToString()));

            return this.SendRequest("video.putTag", callback, list.ToArray());
        }

        /// <summary>
        /// Deletes the video tag.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="owner_id">The user ID. Current user by default. (unnecessary)</param>
        /// <param name="vid">The video ID.</param>  
        /// <param name="tag_id">The tag ID for deleting.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_RemoveTag(long app_id, string sig, string v, long? owner_id, long vid, long tag_id, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("vid", vid.ToString()));
            list.Add(new Parameter("tag_id", tag_id.ToString()));
            if (owner_id != null) list.Add(new Parameter("owner_id", owner_id.ToString()));

            return this.SendRequest("video.removeTag", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the server info and video info .
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="name">The name of the video. (unnecessary)</param>
        /// <param name="description">The description of the video. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Upload object info</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Video_Save(long app_id, string sig, string v, string name, string description, ApiCallback<VideoUploadServer> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig.ToString()));
            list.Add(new Parameter("v", v));
            if (name != null) list.Add(new Parameter("name", name.ToString()));
            if (description != null) list.Add(new Parameter("description", description.ToString()));

            return this.SendRequest("video.save", callback, list.ToArray());
        }
    }
}
